<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" size="default" label-width="80px">
				<el-row :gutter="35">
					
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12"  class="mb20">
						<el-form-item label="文章图片" >
							<Uploadimg v-model="state.ruleForm.image" :value="state.ruleForm.image" @upload="onUpload" :msg="state.msg"/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="文章分类" prop="type">
							<el-select style="width: 100%;" v-model="state.ruleForm.type">
								<el-option :label="item.name" :value="item.id" v-for="item in lists" :key="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="文章标题" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入文章标题" clearable></el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="文章内容" prop="content" placeholder="请输入文章内容" class="w100">
							<textarea style="width: 100%;min-height: 300px; padding: 10px;" v-model="state.ruleForm.content" ></textarea>
						</el-form-item>
					</el-col>
					
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuForm">
import { defineAsyncComponent, reactive, onMounted, ref ,unref,nextTick} from 'vue';
import {save,edit } from '/@/api/alliance/index';
import { ElMessage } from 'element-plus';

import { getList } from '/@/api/article/index';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const Uploadimg = defineAsyncComponent(() => import('/@/components/Upload/singleImage.vue'));

// 引入组件
const IconSelector = defineAsyncComponent(() => import('/@/components/iconSelector/index.vue'));
const onUpload = (data:any) => {
	state.ruleForm.image ='https://st.huayizixun.online'+ data;
};

// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id:0,
		image: '', // 上级菜单
		type: '', // 菜单类型
		name: '', // 菜单名称
		content: '', // 组件路径
		
	},
	menuData: [] , // 上级菜单数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	// 表单校验
	ruleRules: {
		name: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
		content: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }],
	},
	loading: false,
});
const lists = ref([])

const getTypeList = () =>{
	getList().then((res:any) => {
		if(res.code == 200) {
		console.log('dsadasdada',res)

			lists.value = res.data.data
		}
	})
}
// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		// 实际请走接口
		getinfo(row.id).then((response: any) => {
			state.ruleForm = response.data;
		});
		
		state.dialog.title = '修改';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dialogFormRef.value.resetFields();
			initForm();
		});
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;

};
// 关闭弹窗
const closeDialog = () => {
	initForm();
	state.dialog.isShowDialog = false;
};
// 是否内嵌下拉改变
const onSelectIframeChange = () => {
	if (state.ruleForm.is_iframe) state.ruleForm.is_link = 1;
	else state.ruleForm.is_link = 0;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {

	const formWrap = unref(dialogFormRef) as any;
	if (!formWrap) return;
	formWrap.validate((valid: boolean) => {
		if (valid) {
			state.loading = true;
			if(state.ruleForm.parent_id != 0 &&  typeof(state.ruleForm.parent_id) == 'object'){
				state.ruleForm.parent_id = state.ruleForm.parent_id.pop();
			}
			console.log('state.dialog.submitTxt',state.dialog.submitTxt)
			if(state.dialog.submitTxt == '新 增') {
				save(state.ruleForm).then(() => {
					ElMessage.success('保存成功');
					state.loading = false;
					closeDialog(); // 关闭弹窗
					emit('refresh');
				}).finally(() => {
					state.loading = false;
				});
				return
			}
			edit(state.ruleForm).then(() => {
				ElMessage.success('修改成功');
				state.loading = false;
				closeDialog(); // 关闭弹窗
				emit('refresh');
			}).finally(() => {
					state.loading = false;
				});

		}
	})
};
// 页面加载时
onMounted(() => {
	getTypeList()
});


// 表单初始化，方法：`resetFields()` 无法使用
const initForm = () => {
	state.ruleForm.id = 0;
	state.ruleForm.image = '';
	state.ruleForm.name = '';
	state.ruleForm.content = '';
	state.ruleForm.type = '';
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
